package downloader;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;

public class Resource {
    private String url;
    private File file;
    private int size = -1;
    private int threadNum;

    private String fileName;
    private ArrayList<Part> parts = new ArrayList<>();

    public Resource(String url, String savePath, String fileName, int threadNum) {
        this.url = url;
        this.threadNum = threadNum;
        this.fileName = fileName;
        this.file = new File(savePath + File.separator + fileName);
    }

    // Acquire the size of the file
    public int getSize() throws IOException {
        if (size == -1) {
            URL resourceURL = new URL(url);
            HttpURLConnection urlConnection = (HttpURLConnection) resourceURL.openConnection();
            urlConnection.connect();
            size = urlConnection.getContentLength();
            urlConnection.disconnect();
        }
        return size;
    }

    public File getFile() {
        return file;
    }

    public int getThreadNum() {
        return threadNum;
    }

    public void addPart(Part p) {
        parts.add(p);
    }

    public String getFileName() {
        return fileName;
    }

    public String getUrl() {
        return url;
    }
}
